package games.ninJump.ui.effects
{
	import core.Global;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import games.ninJump.NinJump;
	import games.ninJump.data.Animations;
	import games.ninJump.data.GameState;
	import games.ninJump.ui.actions.ActionTrans;
	
	import interfaces.IUpdate;
	
	import utils.Particle;
	
	public class MakeEnemies extends Sprite implements IUpdate
	{
		private var m_gameState:GameState;
		private var m_enemys:Vector.<Particle>;
		private var m_multiple:Number = .25;
		private var m_speed:Number;
		private var m_count:int;
		
		public function MakeEnemies(gs:GameState)
		{
			m_enemys = new Vector.<Particle>();
			m_gameState = gs;
			speed = m_gameState.speed * m_multiple;
			m_gameState.addEventListener(Event.CHANGE,onChangeState);
			
		}
		
		public function update(time:Object=null):void
		{
			var t:Number = Number(time);
			var temp:int = int(t / 2500);
			
			if(temp != m_count)
			{
				var n:Number = Math.random();
				if(n > .7)
					makeMoveEnemy();
				else if(n > .4)
					makeTowerEnemy();
				else if(n > .3)
					makeBridge();
				else if(n > .2)
					makeChain();
				m_count = temp;
				//trace("count.............",m_count);
		
			}
			if(m_enemys && m_enemys.length > 0)
			for(var i:int = 0; i < m_enemys.length; i++)
			{
				if(m_enemys[i].clip.y >= GameState.GAME_HEIGHT)
				{
					Particle(m_enemys.splice(i,1)[0]).destroy();
					i--;
					continue;
				}
				m_enemys[i].clip.y = (t % 2500) * m_speed - 200 ;	
				
				//	m_enemys[i].setPos(GameState.LEFT_SIDE,(t % 2500) * m_speed - 200 );	
					
				
			}
		}
		
		
		public function set speed(n:Number):void
		{
			m_speed = n;
		}
		
		private function onChangeState(e:Event):void
		{
			speed = GameState(e.target).speed * m_multiple ;
		}
		
		private function createActionTrans(name:String,test:Boolean = true):Sprite
		{ 
			// test 是否检测碰撞
			var enemy:ActionTrans = new ActionTrans();
			Global.addAction(enemy);
			var atom:* = Global.loadResource(NinJump.GAME_URL,name);
			var bmd:BitmapData = atom.obj;
			enemy.des = Animations.getAnimationByName(name);
			enemy.bmd = bmd;
			if(test)
			GameState.ENEMIES.push(enemy);
			return enemy.target;
		}
		
		private function makeMoveEnemy():void
		{
			var name:String = GameState.ENEMY_NAMES[int(Math.random() *  GameState.ENEMY_NAMES.length)];
			trace("name.............",name);
			var enemy:Sprite = createActionTrans(name);
			var particle:Particle = new Particle(enemy,this);
			//enemy.y = - 200;
			if(Math.random() > .5)
			{
				particle.setPos(GameState.LEFT_SIDE,-200);				
			}else
			{	
				particle.setPos(GameState.RIGHT_SIDE,-200);					
				particle.clip.scaleX = -1;
			}
			m_enemys.push(particle);
		}
		
		private function makeTowerEnemy():void
		{
			var name:String = GameState.ENEMY_TOWERS[int(Math.random() *  GameState.ENEMY_TOWERS.length)];
			trace("name.............",name);
			var enemy:Sprite = createActionTrans(name);
			var particle:Particle = new Particle(enemy,this);
			//enemy.y = - 200;
			if(Math.random() > .5)
			{
				particle.setPos(0,-200);				
			}else
			{	
				particle.setPos(GameState.GAME_WIDTH,-200);					
				particle.clip.scaleX = -1;
			}
			m_enemys.push(particle);
		}
		
		private function makeBridge():void
		{
			var name:String = "bridge";
			var enemy:Sprite = createActionTrans(name,false);
			var particle:Particle = new Particle(enemy,m_gameState.target.bg);
			particle.setPos((GameState.GAME_WIDTH - enemy.width)/2,-200);
			m_enemys.push(particle);
		}
		
		private function makeChain():void
		{
			var name:String = "chain";
			var enemy:Sprite = createActionTrans(name,false);
			var particle:Particle = new Particle(enemy,m_gameState.target.bg);
			particle.setPos((GameState.GAME_WIDTH - enemy.width)/2,-200);
			m_enemys.push(particle);
		}
	}
}